<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/5 0005
 * Time: 14:36
 */
function removeXSS($data)
{
    require_once './HtmlPurifier/HTMLPurifier.auto.php';
    $_clean_xss_config = HTMLPurifier_Config::createDefault();
    $_clean_xss_config->set('Core.Encoding', 'UTF-8');
    $_clean_xss_config->set('HTML.Allowed', 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]');
    $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
    $_clean_xss_config->set('HTML.TargetBlank', TRUE);
    $_clean_xss_obj = new HTMLPurifier($_clean_xss_config);
    return $_clean_xss_obj->purify($data);
}


function deleteImage($image = array())
{
    $savePath = C('IMAGE_CONFIG');
    foreach ($image as $item) {
        unlink($savePath['rootPath'] . $item);
    }
}

/**上传图片并生成缩略图
 * @param $imgName
 * @param $dirName
 * @param array $thumb
 */
function uploadOne($imgName, $dirName, $thumb = array())
{
    if (isset($_FILES[$imgName]) && $_FILES[$imgName]['error'] == 0) {
        $ic = C('IMAGE_CONFIG');
        $upload = new Think\Upload(
            array(
                'rootPath' => $ic['rootPath'],
                'maxSize' => $ic['maxSize'],
                'exts' => $ic['exts']
            )
        );
        $upload->savePath = $dirName . '/';
        $info = $upload->upload(array($imgName => $_FILES[$imgName]));
        if (!$info) {
            return array(
                'ok' => 0,
                'error' => $upload->getError(),
            );
        } else {
            $ret['ok'] = 1;
            $ret['images'][0] = $logoName = $info[$imgName]['savepath'] . $info[$imgName]['savename'];
            if ($thumb) {
                $image = new Think\Image();
                foreach ($thumb as $k => $v) {
                    $ret['images'][$k + 1] = $info[$imgName]['savepath'] . 'thumb_' . $k . "_" . $info[$imgName]['savename'];
                    $image->open($ic['rootPath'] . $logoName);
                    $image->thumb($v[0], $v[1])->save($ic['rootPath'] . $ret['images'][$k + 1]);

                }
            }
            return $ret;
        }


    }
}

function showImage($url, $width = '', $height = '')
{
    $ic = C('IMAGE_CONFIG');
    if ($width)
        $width = "width='$width'";
    if ($height)
        $height = "height='$height'";
    echo "<img id='show' $width $height src='{$ic['viewPath']}$url' />";
}

function buildSelect1($tableName, $selectName, $valueFieldName, $textFieldName, $selectedValue = '')
{
    $moudle = D($tableName);
    $data = $moudle->field("$valueFieldName,$textFieldName")->select();
    $select = "<select name='$selectName'><option value=''>请选择</option>";
    foreach ($data as $item) {
        $value = $item[$valueFieldName];
        $text = $item[$textFieldName];
        if ($selectedValue && $selectedValue == $value) {
            $selected = 'selected="selected"';
        } else {
            $selected = '';
        }
        $select = $selected . '<option ' . $selected . 'value =' . $value . '>' . $text . '</option>';
    }
    $select .= '</select>';
    echo $select;
}

/**
 * 使用一个表中的数据制作下拉框
 *
 */
function buildSelect($tableName, $selectName, $valueFieldName, $textFieldName, $selectedValue = '')
{
    $model = D($tableName);
    $data = $model->field("$valueFieldName,$textFieldName")->select();
    $select = "<select name='$selectName'><option value=''>请选择</option>";
    foreach ($data as $k => $v) {
        $value = $v[$valueFieldName];
        $text = $v[$textFieldName];
        if ($selectedValue && $selectedValue == $value)
            $selected = 'selected="selected"';
        else {
            $selected = '';
        }
        $select .= '<option ' . $selected . ' value="' . $value . '">' . $text . '</option>';
    }
    $select .= '</select>';
    echo $select;
}